package impl.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;

import classes.Constants;
import classes.Utils;

public class ParametersWindow extends JFrame implements ActionListener {
	private final String TAG = Constants.TAG + "." + getClass().getSimpleName();

	private static final long serialVersionUID = 8596286284455387471L;
	
	private JTextField inputRobots;
	
	private JTextField inputCandies;
	
	private int robots = 0;
	
	private int candies = 0;

	public ParametersWindow() {
		Utils.logC(TAG, "Creating window");

		int screenHeight = getToolkit().getScreenSize().height;
		int screenWidth = getToolkit().getScreenSize().width;

		setLayout(new GridBagLayout());
		GridBagConstraints labelConstraints = new GridBagConstraints();
		labelConstraints.fill = GridBagConstraints.HORIZONTAL;
		labelConstraints.gridx = 0;
		labelConstraints.gridy = 0;
		labelConstraints.weightx = 0.3;
		labelConstraints.insets = new Insets(5, 10, 5, 10);
		
		GridBagConstraints inputConstraints = new GridBagConstraints();
		inputConstraints.fill = GridBagConstraints.HORIZONTAL;
		inputConstraints.gridx = 1;
		inputConstraints.gridy = 0;
		inputConstraints.weightx = 1;
		inputConstraints.insets = new Insets(5, 10, 5, 10);

		// Number of robots Label
		JLabel labelRobots = new JLabel("Number of Robots:");
		add(labelRobots, labelConstraints);
		
		// Number of robots Input
		inputRobots = new JTextField();
		((AbstractDocument) inputRobots.getDocument()).setDocumentFilter(new Utils.NumberFilter(3));
		inputRobots.setFont(inputRobots.getFont().deriveFont(20f));
		inputRobots.setHorizontalAlignment(JTextField.CENTER);
		add(inputRobots, inputConstraints);
		
		// Number of candies Label
		JLabel labelCandies = new JLabel("Number of Candies:");
		labelConstraints.gridy = 1;
		add(labelCandies, labelConstraints);
		
		inputCandies = new JTextField();
		((AbstractDocument) inputCandies.getDocument()).setDocumentFilter(new Utils.NumberFilter(3));
		inputCandies.setFont(inputCandies.getFont().deriveFont(20f));
		inputCandies.setHorizontalAlignment(JTextField.CENTER);
		inputConstraints.gridy = 1;
		add(inputCandies, inputConstraints);
		
		JButton buttonCreate = new JButton("Create Candy World");
	    buttonCreate.addActionListener(this);
	    
	    inputConstraints.gridwidth = 2;
	    inputConstraints.gridx = 0;
	    inputConstraints.gridy = 2;
	    add(buttonCreate, inputConstraints);
		
		// Rest of windows settings
		setLocation(screenWidth / 2 - Constants.PARAMS_WIDTH / 2, screenHeight / 2 - Constants.PARAMS_HEIGHT / 2);
		setVisible(true);
		setSize(Constants.PARAMS_WIDTH, Constants.PARAMS_HEIGHT);
		setResizable(false);
		setTitle(Constants.TAG);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		try {
			robots = Integer.parseInt(inputRobots.getText());
		} catch (NumberFormatException e) {
			Utils.logC(TAG, "NumberFormatError: " + e.getMessage());
			JOptionPane.showMessageDialog(this, "Invalid number of robots. Please try again!", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		try {
			candies = Integer.parseInt(inputCandies.getText());
		} catch (NumberFormatException e) {
			Utils.logC(TAG, "NumberFormatError: " + e.getMessage());
			JOptionPane.showMessageDialog(this, "Invalid number of candies. Please try again!", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		Utils.logC(TAG, "Returning values: " + robots + " | " + candies);
		dispose();
	}

	public int getCandiesNumber() {
		return candies;
	}
	
	public int getRobotsNumber() {
		return robots;
	}
}
